package com.yunysha.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yunysha.controller.YunyshaController;

/**
 * Servlet implementation class SearchFilterServlet
 */
public class SearchFilterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchFilterServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		if (request.getParameter("parameters") != null) {
			String parameters = request.getParameter("parameters");
			String responseXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

			PrintWriter out = response.getWriter();
			response.setContentType("text/xml");
			response.setHeader("Cache-Control", "no-cache");

			YunyshaController yController = new YunyshaController();
			try {
				if (parameters != null && !"".equals(parameters)) {
					String[] splitParams = parameters.split(",##,");
					if ("userList".equals(splitParams[0])) {
						responseXML = responseXML
								+ yController.retrieveUsersSearch(
										splitParams[1], splitParams[2],
										splitParams[3],
										Integer.parseInt(splitParams[4]));
					}
				}
				out.write(responseXML);
			} catch (Exception ex) {

			} finally {
				if (out != null) {
					out.flush();
					out.close();
				}
			}
		}
	}

}
